package com.pangolin.pangolinim.ui.fragment;

import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lqr.optionitemview.OptionItemView;
import com.orhanobut.logger.Logger;
import com.pangolin.pangolinim.R;
import com.pangolin.pangolinim.app.BroadcastMsg;
import com.pangolin.pangolinim.ui.activity.AccAndSafeActivity;
import com.pangolin.pangolinim.ui.activity.MainActivity;
import com.pangolin.pangolinim.ui.activity.SettingActivity;
import com.pangolin.pangolinim.ui.activity.ShowBigImageActivity;
import com.pangolin.pangolinim.ui.adapter.UIContact;
import com.pangolin.pangolinim.ui.base.BaseFragment;
import com.pangolin.pangolinim.utils.BundleUtil;
import com.pangolin.pangolinim.utils.GsonUtil;
import com.zhy.autolayout.AutoRelativeLayout;

import java.util.HashMap;
import java.util.Map;

public class MeFragment extends BaseFragment {
    private OptionItemView mOivCto;
    private OptionItemView mOivCsdn;
    private OptionItemView mOivGitee;
    private OptionItemView mOivSetting;
    private LinearLayout mLlMyInfo;

    private TextView tvName;
    private TextView tvUid;
    private ImageView ivHeadimg;

    @Override
    public void init() {
        MainActivity activity = (MainActivity) getActivity();
        //检查头像图片，如果没有就开启异步下载
        if(!TextUtils.isEmpty(activity.me.getHeader())  && activity.me.getBitHeader() == null){
            Logger.d("download pic:%s" + activity.me.getHeader());
            /** 异步下载头像图片 */
            Map<String, Object> brocastMap = new HashMap<String, Object>();
            brocastMap.put("type" , BroadcastMsg.DATA_UI_DOWNPIC);
            brocastMap.put("uid" , activity.me.getUid());
            brocastMap.put("filename" , activity.me.getHeader());
            BundleUtil bundleUtil = new BundleUtil(BroadcastMsg.SIG_UI_DATA , brocastMap , null);
            activity.BroadcastSender(bundleUtil.getBundle());
        }
    }

    @Override
    public void initView(View rootView) {
        mOivCto = rootView.findViewById(R.id.oivCto);
        mOivCsdn = rootView.findViewById(R.id.oivCsdn);
        mOivGitee = rootView.findViewById(R.id.oivGitee);
        mOivSetting = rootView.findViewById(R.id.oivSetting);
        tvName = rootView.findViewById(R.id.tvName);
        tvUid = rootView.findViewById(R.id.tvAccount);
        ivHeadimg = rootView.findViewById(R.id.ivHeader);
        mLlMyInfo = rootView.findViewById(R.id.llMyInfo);
    }

    @Override
    public void initListener() {
        mOivGitee.setOnClickListener(v -> ((MainActivity) getActivity()).jumpToWebViewActivity("https://gitee.com/yiyefangzhou24"));
        mOivCsdn.setOnClickListener(v -> ((MainActivity) getActivity()).jumpToWebViewActivity("https://blog.csdn.net/yiyefangzhou24"));
        mOivCto.setOnClickListener(v -> ((MainActivity) getActivity()).jumpToWebViewActivity("https://blog.51cto.com/yiyefangzhou24"));
        mOivSetting.setOnClickListener(v -> {
            ((MainActivity) getActivity()).jumpToActivity(SettingActivity.class, GsonUtil.toJson(((MainActivity)getActivity()).me));
        });
        mLlMyInfo.setOnClickListener(v -> {
            ((MainActivity) getActivity()).jumpToActivity(AccAndSafeActivity.class, GsonUtil.toJson(((MainActivity)getActivity()).me));
        });
        ivHeadimg.setOnClickListener(v -> {
            ((MainActivity) getActivity()).jumpToActivity(ShowBigImageActivity.class, GsonUtil.toJson(((MainActivity)getActivity()).me) , ((MainActivity)getActivity()).me.getHeader());
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        //Logger.d("MeFragment onResume");
        // 发送获取个人信息请求
        Map<String, Object> brocastMap = new HashMap<String, Object>();
        brocastMap.put("type" , BroadcastMsg.DATA_UI_MYINFO);
        BundleUtil bundleUtil = new BundleUtil(BroadcastMsg.SIG_UI_DATA , brocastMap , null);
        ((MainActivity)getActivity()).BroadcastSender(bundleUtil.getBundle());
    }

    @Override
    public void onPause() {
        super.onPause();
        //Logger.d("MeFragment onPause");
    }

    @Override
    public void initData() {
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.fragment_me;
    }

    public void setMyInfo(UIContact me){

        Logger.d("me:%d|%s|%s" , me.getUid() , me.getName() , me.getHeader());
        tvName.setText(me.getName());
        tvUid.setText("UID:"+String.valueOf(me.getUid()));
        if(me.getBitHeader() != null){
            ivHeadimg.setImageBitmap(((MainActivity)getActivity()).me.getBitHeader());
        }
    }

}